package main

import(
    "fmt"
    "./file"
    "flag"
    "os"
)

func GetHash(f *file.File, force_internal bool, c chan *file.File){
    f.CalcMD5(force_internal)
    c <- f
}
//TODO: add flag for choosing linear or parallel
func main(){
    do_linear := flag.Bool("l", false, "execute in linear")
    force_internal := flag.Bool("i", false, "force internal md5 algorithm")
    flag.Parse()
    fnames := flag.Args()
    files := make([]*file.File, len(fnames))
    c := make(chan *file.File)
    var err os.Error
    for i, _ := range fnames{
        files[i], err = file.Open(fnames[i], 0, 0)
        if err != nil{
            fmt.Printf("error opening file: %s\n", err.String())
        }
        // linear
        if *do_linear == true{
            files[i].CalcMD5(*force_internal)
            fmt.Printf("md5 hash for \"%s\": %s\n", files[i].String(), files[i].MD5())
        }else{
        //concurrent
            go GetHash(files[i], *force_internal, c)  //do them concurrently
        }
    }
    if !*do_linear{
        for i := 0 ; i < len(files); i++{ 
            f := <-c
            fmt.Printf("md5 hash for \"%s\": %s\n", f.String(), f.MD5())
        }
    }
}
